
package com.relax.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

@Service

public class KafkaProducerService {  

  

    @Autowired

    private KafkaTemplate<String, String> kafkaTemplate;

  

    public void sendMessage(String topic, String message) {  
        // 异步发送消息
        kafkaTemplate.send(topic, message).addCallback(success -> {
            System.out.println("Message sent successfully!");
        }, failure -> {
            System.err.println("Failed to send message: " + failure.getMessage());

        });  

    }  

}
